
$(window).load(function(){
	loadMap();
	if (typeof 'find-store-form' == 'object' && navigator.geolocation ) {
		  navigator.geolocation.getCurrentPosition(success);
	} 
	mapRender();
});

function mapRender(){

	deleteOverlays();
	$(".loc").each(
		function(){
			// Define Marker properties
		    var pin = new google.maps.MarkerImage($(this).attr('pin'),
		        // This marker is 129 pixels wide by 42 pixels tall.
		        new google.maps.Size(77, 100),
		        // The origin for this image is 0,0.
		        new google.maps.Point(0,0),
		        // The anchor for this image is the base of the flagpole at 18,42.
		        new google.maps.Point(18, 42)
		    );
			var marker;
			var latlng = new google.maps.LatLng($(this).attr('latitude'),$(this).attr('longitude'));
		    marker = new google.maps.Marker({
		    	position: latlng,
		        map: map,
		        title: $(this).attr('title'),
		        icon: pin
		    });
		    
		    markers.push(marker);
		});
//	if (coords) {
//		var marker = new google.maps.Marker({
//	    	position: coords,
//	        map: map,
//	        title: 'You are here',
//	        icon: '/images/template/pin.png'
//	    });
//		markers.push(marker);
//	}
	
	resetBounds();
}

var map;
var markers = [];
var bound = new google.maps.LatLngBounds();
var coords = null;

function loadMap() {
	var wid=jQuery(window).width();
	var isDesktop = true;
    if (wid<767) isDesktop = false; 
	var startLatlng = new google.maps.LatLng(-34.9201702,138.5943725);
	var options = {
	    zoom: 12,
	    center: startLatlng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP,
	    panControl: true,
	    maxZoom: 19,
	    minZoom: 0,
	    draggable: isDesktop,
	    scrollwheel: false
	  };
	map = new google.maps.Map(document.getElementById("map-canvas"),options);
}

function deleteOverlays() {
	 if (markers) {
	   for (i in markers) {
		   markers[i].setMap(null);
	   }
	   markers.length = 0;
	 }
}

function resetBounds() {
	bound = new google.maps.LatLngBounds();
	for(var i in markers)
	{
		bound.extend(markers[i].getPosition());
	}
	map.fitBounds(bound);
}

function findStore(){
	$('body').css('cursor','wait');
	$('#states-wrapper').hide();
	var datastring = $("#find-store-form").serialize();
	$.ajax({
		type: "POST",
	    url: "/process/general",
		cache: false,
		data: 'action=FindStore&' + datastring,
		dataType: "html",
	    success: function(data) {
	    	try{
	    		var obj = $.parseJSON(data);
	    		$('#location-wrapper').html(obj.html);
	    		scrolltodiv('#mapbox');
	    		mapRender();
			}catch(err){
			}
			$('body').css('cursor','default'); 
	    },
		error: function(){
			$('body').css('cursor','default'); 
      	}
	});
}



function success(position) {
    coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    $('body').css('cursor','wait');
	$.ajax({
		type: "POST",
	    url: "/process/general",
		cache: false,
		data: 'action=FindStore&lat='+position.coords.latitude+'&lon='+position.coords.longitude+"&limit=3",
		dataType: "html",
	    success: function(data) {
	    	try{
	    		var obj = $.parseJSON(data);
	    		$('#location-wrapper').html(obj.html);
	    		scrolltodiv('#mapbox');
	    		mapRender();
			}catch(err){
			}
			$('body').css('cursor','default'); 
	    },
		error: function(){
			$('body').css('cursor','default'); 
      	}
	});
}

function mapInNewWindow() {
	var mapCenter = new String(map.getCenter());
	window.open("https://www.google.com.au/maps/search/chemplus+in+australia/@" + mapCenter.replace(/[\s()]/g,'') +"," + map.getZoom() + "z","_blank");
	
}
